/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */

package configuration;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.apache.logging.log4j.web.Log4jServletContextListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.util.IntrospectorCleanupListener;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * ServletAppInitializer
 * @author limk
 * @date 2018年2月10日 下午9:18:05
 * @version 1.0
 */
public class ServletAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {

		// 加载log4j2监听器和日志配置
		servletContext.setInitParameter("log4jConfiguration", "classpath:log4j2.xml");
		servletContext.addListener(Log4jServletContextListener.class);

		// 加载Introspector缓存清除监听器和request监听器
		servletContext.addListener(IntrospectorCleanupListener.class);
		servletContext.addListener(RequestContextListener.class);
		
		// 加载DruidWebStat过滤器
        FilterRegistration.Dynamic druidWebStatFilter = servletContext.addFilter("DruidWebStatFilter", WebStatFilter.class);
        druidWebStatFilter.setInitParameter("exclusions", "/bulid/*,/images/*,/plugins/*,*.ico,/druid/*");
        druidWebStatFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
				DispatcherType.INCLUDE, DispatcherType.ERROR), false, "/*");
        
        // 加载StatView调度器
        ServletRegistration.Dynamic druidStatView = servletContext.addServlet("DruidStatView", StatViewServlet.class);
        druidStatView.setInitParameter("loginUsername", "admin");
        druidStatView.setInitParameter("loginPassword", "123");
        druidStatView.addMapping("/druid/*");
        
		super.onStartup(servletContext);
	}

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return new Class<?>[] {RootConfig.class, TransactionConfig.class, ShiroConfig.class};  
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] {WebMvcConfig.class};  
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"}; 
	}

	@Override
	protected Filter[] getServletFilters() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		
		// 加载shiro过滤器
		DelegatingFilterProxy shiroFilter = new DelegatingFilterProxy();
		shiroFilter.setTargetBeanName("shiroFilter");
		shiroFilter.setTargetFilterLifecycle(true);
		return new Filter[] { characterEncodingFilter, shiroFilter };
	}

	@Override
	protected void customizeRegistration(ServletRegistration.Dynamic registration) {
		registration.setInitParameter("defaultHtmlEscape", "true");
		registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
	}

}
